﻿using CPPEI.Coolzon.UI.Script;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI
{
    public partial class ScenePart
    {
        /// <summary>
        /// viewstate:area pos
        /// </summary>
        public const string VS_AREAPOS = "areapos";

        /// <summary>
        /// viewstate:horizontal loop offset
        /// </summary>
        public const string VS_LOOPH = "looph";

        public override void LoadViewState(Coolzon.ViewState.ViewState viewState)
        {
            Coolzon.ViewState.PartState partState = viewState.GetPartState(this.XFullName);

            if (partState != null)
            {
                #region load areapos
                string areaposString = partState.GetString(VS_AREAPOS);
                if (!string.IsNullOrEmpty(areaposString))
                {
                    string[] areaposArr = areaposString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int index = 0; index < areaposArr.Length; index++)
                    {
                        string[] areaposSegments = areaposArr[index].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

                        for (int indexArea = 0; indexArea < this._subAreas.Count; indexArea++)
                        {
                            UI.AreaPart areaPart = this._subAreas[indexArea];
                            if (areaPart.Name == areaposSegments[0])
                            {
                                areaPart.XLeft = Common.Convertor.ToDouble(areaposSegments[1]);
                                this.ChildrenPosInfo.Set(areaPart.Name, areaPart.XLeft);
                                break;
                            }
                        }
                    }
                }
                #endregion

                #region load looph
                string loopHString = partState.GetString(VS_LOOPH);
                if (!string.IsNullOrEmpty(loopHString))
                {
                    this.HorizontalLoop(Common.Convertor.ToDouble(loopHString));
                }
                #endregion
            }

            #region load sub part viewstate
            for (int index = 0; index < this._partCollection.Count; index++)
            {
                this._partCollection[index].LoadViewState(viewState);
            }
            #endregion

            base.LoadViewState(viewState);
        }

        public override void SaveViewState(Coolzon.ViewState.ViewState viewState)
        {
            Coolzon.ViewState.PartState partState = viewState.GetPartState(this.XFullName);
            if (partState == null)
            {
                partState = new Coolzon.ViewState.PartState(this.XFullName);
                viewState.AddPartState(partState);
            }

            #region save areapos
            StringBuilder areaposBuilder = new StringBuilder();
            for (int index = 0; index < this._subAreas.Count; index++)
            {
                UI.AreaPart areaPart = this._subAreas[index];
                areaposBuilder.AppendFormat("{0}:{1},", areaPart.Name, areaPart.XLeft);
            }
            partState.SetString(VS_AREAPOS, areaposBuilder.ToString());
            #endregion

            #region save looph
            partState.SetString(VS_LOOPH, this.CurrentHLoopOffset.ToString());
            #endregion

            #region save sub part viewstate
            for (int index = 0; index < this._partCollection.Count; index++)
            {
                UI.Part subPart = this._partCollection[index];
                subPart.SaveViewState(viewState);
            }
            #endregion
        }

        /// <summary>
        /// 清楚视图状态
        /// </summary>
        /// <param name="viewState"></param>
        public void ResetViewState(Coolzon.ViewState.ViewState viewState)
        {
            viewState.RemovePartState(this.XFullName);

            #region save sub part viewstate
            for (int index = 0; index < this._partCollection.Count; index++)
            {
                UI.Part subPart = this._partCollection[index];
                UI.IPartContainer subPartContainer = subPart as IPartContainer;
                if (subPartContainer != null)
                {
                    ResetSubViewState(viewState, subPart, subPartContainer);
                }
            }
            #endregion
        }

        /// <summary>
        /// 清楚子组件的视图状态
        /// </summary>
        /// <param name="viewState"></param>
        /// <param name="subPart"></param>
        /// <param name="subPartContainer"></param>
        private void ResetSubViewState(Coolzon.ViewState.ViewState viewState, UI.Part subPart, UI.IPartContainer subPartContainer)
        {
            viewState.RemovePartState(subPart.XFullName);

            #region save sub part viewstate
            List<UI.Part> nextSubPartList = subPartContainer.GetPartList();
            for (int index = 0; index < nextSubPartList.Count; index++)
            {
                UI.Part nextSubPart = nextSubPartList[index];
                UI.IPartContainer nextSubPartContainer = nextSubPart as IPartContainer;
                if (nextSubPartContainer != null)
                {
                    ResetSubViewState(viewState, nextSubPart, nextSubPartContainer);
                }
            }
            #endregion
        }
    }
}
